import { FormSchema } from '/@/components/Table';
import { BasicColumn } from '/@/components/Table';
import { formatToDateTime } from '/@/utils/dateUtil';
import { ProductsServiceProxy, PageProductInputDto,IdInput } from '/@/services/ServiceProxies';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n();
 
export const searchFormSchema: FormSchema[] = [
  {
    field: 'filter',
    label: t('common.key'),
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const tableColumns: BasicColumn[] = [
  {
    title: t('routes.basic.productName'),
    dataIndex: 'productName',
  },
  {
    title: t('routes.basic.productCode'),
    dataIndex: 'productCode',
  },
  {
    title: t('routes.basic.description'),
    dataIndex: 'description',
  },
  {
    title: t('routes.basic.creator'),
    dataIndex: 'creatorName',
  },
  {
    title: t('common.creationTime'),
    dataIndex: 'creationTime',
    customRender: ({ text }) => {
      return formatToDateTime(text);
    }
  }
];

// 创建产品 FormSchema
export const createFormSchema: FormSchema[] = [
  {
    field: 'productName',
    label:  t('routes.basic.productName'),
    component: 'Input',
    required: true,
    colProps: { span: 18 },
  },
  {
    field: 'productCode',
    label: t('routes.basic.productCode'),
    component: 'Input',
    required: true,
    colProps: { span: 18 },
  },
  {
    field: 'description',
    label: t('routes.basic.description'),
    component: 'Input',
    required: false,
    colProps: { span: 18 },
  }
];

// 编辑产品 FormSchema
export const updateFormSchema: FormSchema[] = [
  {
    field: 'id',
    label: 'Id',
    component: 'Input',
    ifShow: false,
    colProps: { span: 18 },
  },
  {
    field: 'productName',
    label:  t('routes.basic.productName'),
    component: 'Input',
    required: true,
    colProps: { span: 18 },
  },
  {
    field: 'productCode',
    label:  t('routes.basic.productCode'),
    component: 'Input',
    required: true,
    colProps: { span: 18 },
  },
  {
    field: 'description',
    label:  t('routes.basic.description'),
    component: 'Input',
    colProps: { span: 18 },
  }
];

export async function getTableListAsync(params: PageProductInputDto) {
  const productsServiceProxy = new ProductsServiceProxy();
  return productsServiceProxy.page(params);
}

/**
 * 创建产品
 */
export async function createAsync({ params }) {
  const productsServiceProxy = new ProductsServiceProxy();
  await productsServiceProxy.create(params);
}

/**
 * 更新产品
 */
export async function updateAsync({ params }) {
  const productsServiceProxy = new ProductsServiceProxy();
  await productsServiceProxy.edit(params);
}

/**
 * 删除产品
 */
export async function deleteAsync({ id }) {
  const productsServiceProxy = new ProductsServiceProxy();
  const request = new IdInput ();
  request.id = id;
  await productsServiceProxy.delete(request);
}
